import { roleList } from '../role/data'
import { userList } from '../user/data'
import { menuList } from '../menu/data'

export default [
  {
    url: RegExp('/system/permission/list-user-roles' + '.*'), // 查询用户拥有的角色
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: returnRoleIds(params)
      }
    }
  },
  {
    url: RegExp('/system/permission/list-role-menus' + '.*'), // 查询角色拥有的菜单权限
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: returnMenuIds(params)
      }
    }
  },
  {
    url: '/system/permission/assign-user-role', // 赋予用户角色
    method: 'post',
    response: data => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/system/permission/assign-role-menu', // 赋予角色菜单权限
    method: 'post',
    response: data => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/system/permission/assign-role-data-scope', // 赋予角色数据权限
    method: 'post',
    response: data => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  }
]

const returnRoleIds = params => {
  let result: any = []
  userList.forEach(item => {
    if (item.id === parseInt(params.query.userId)) {
      if (item.roleIds) {
        if (item.roleIds.indexOf(',') !== -1) {
          item.roleIds.split(',').forEach(v => {
            result.push(parseInt(v))
          })
        } else {
          result.push(parseInt(item.roleIds))
        }
      }
    }
  })
  return result
}

const returnMenuIds = params => {
  let result: any = []
  roleList.forEach(item => {
    if (item.id === parseInt(params.query.roleId)) {
      // 超级管理员拥有所有权限
      if (item.code === 'admin') {
        menuList.forEach(v => {
          if (v.type === 3 || v.type === 2) {
            result.push(parseInt(v.id))
          }
        })
      } else {
        if (item.menuIds) {
          if (item.menuIds.indexOf(',') !== -1) {
            item.menuIds.split(',').forEach(v => {
              result.push(parseInt(v))
            })
          } else {
            result.push(parseInt(item.menuIds))
          }
        }
      }
    }
  })
  return result
}
